package com.xnyzc.lhy.mis.service.impl.manager;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.component.sms.AliCloudSMSUtils;
import com.xnyzc.lhy.common.constant.AliCloudSmsConstant;
import com.xnyzc.lhy.common.constant.RedisPrefixConstant;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.order.EOrderChannelType;
import com.xnyzc.lhy.common.penum.vehicle.ECallVehType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.manager.OaSysManager;
import com.xnyzc.lhy.mis.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.mis.entity.sidebar.OaSysOffice;
import com.xnyzc.lhy.mis.entity.system.OaSysSendMessage;
import com.xnyzc.lhy.mis.entity.system.OaSysUser;
import com.xnyzc.lhy.mis.mapper.manager.OaSysManagerMapper;
import com.xnyzc.lhy.mis.mapper.order.OaCOrderPersonalMapper;
import com.xnyzc.lhy.mis.mapper.sidebar.OaSysOfficeMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysSendMessageMapper;
import com.xnyzc.lhy.mis.mapper.system.OaSysUserMapper;
import com.xnyzc.lhy.mis.service.manager.OaSysManagerService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.internal.http2.ErrorCode;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OaSysManagerServiceImpl implements OaSysManagerService {

    @Autowired
    private OaSysManagerMapper oaSysManagerMapper;

    @Autowired
    private OaSysUserMapper oaSysUserMapper;
    @Autowired
    private OaSysOfficeMapper oaSysOfficeMapper;
    @Autowired
    private OaCOrderPersonalMapper oaCOrderPersonalMapper;
    @Autowired
    private OaSysSendMessageMapper oaSysSendMessageMapper;
    @Autowired
    private RedisCommon redisCommon;

    @Override
    public Rv getManagerInfo(Long cityId, Boolean isTopManagement) {
        Long userId = TokenUtil.getTokenUserId();
        //根据用户id查询所属供应商
        QueryWrapper<OaSysUser> oaSysUserQueryWrapper = new QueryWrapper<>();
        oaSysUserQueryWrapper.eq(OaSysUser.SYS_USER_ID, userId);
        OaSysUser oaSysUser = oaSysUserMapper.selectOne(oaSysUserQueryWrapper);
        OaSysSendMessage oaSysSendMessage = new OaSysSendMessage();
        if (CheckUtil.strIsNotEmpty(oaSysUser.getSysUserId().toString())) {
            QueryWrapper<OaSysSendMessage> oaSysSendMessageQueryWrapper = new QueryWrapper<>();
            oaSysSendMessageQueryWrapper.eq(OaSysSendMessage.OFFICE_ID, oaSysUser.getOfficeId());
            oaSysSendMessage=oaSysSendMessageMapper.selectOne(oaSysSendMessageQueryWrapper);
        } else {
            return Rv.error(EErrorCode.userFieldError);
        }
        return Rv.wrap(oaSysSendMessage);
        /*QueryWrapper<OaSysManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysManager.DELETED,0);
        if(!isTopManagement){//不是最高管理者
            if (CheckUtil.objIsEmpty(cityId)){
                return Rv.error(EErrorCode.missingArg);
            }
            if(cityId > 0){
                queryWrapper.eq(OaSysManager.CITY_ID,cityId);
            }
        }
        List<OaSysManager> oaSysManagerList = oaSysManagerMapper.selectList(queryWrapper);
        if(oaSysManagerList.size() <= 0){
            return Rv.error(EErrorCode.infoIsNull);
        }
        if(!isTopManagement){//不是最高管理者
            //每一个城市只有一条司管信息
            OaSysManager oaSysManager = oaSysManagerList.get(0);
            return Rv.wrap(oaSysManager);
        }else {//最高管理者
            return Rv.wrap(oaSysManagerList);
        }*/
    }

    @Override
    public Rv setManagerInfo(OaSysSendMessage oaSysSendMessage) {
        boolean flag = false;
        if (CheckUtil.objIsEmpty(oaSysSendMessage.getId())) {
            oaSysSendMessage.setId(IDUtil.nextId());
        } else {
            flag = true;
        }
        if (CheckUtil.objIsEmpty(oaSysSendMessage.getManagerPhone())) {
            return Rv.error(EErrorCode.missingArg);
        }
        if (CheckUtil.objIsEmpty(oaSysSendMessage.getNewOrderToManager())) {
            oaSysSendMessage.setNewOrderToManager(0);
        }
        if (CheckUtil.objIsEmpty(oaSysSendMessage.getNewOrderToDriver())) {
            oaSysSendMessage.setNewOrderToDriver(0);
        }
        if (CheckUtil.objIsEmpty(oaSysSendMessage.getUndeliveredEarlyWarning())) {
            oaSysSendMessage.setUndeliveredEarlyWarning(0);
        }
        if (CheckUtil.objIsEmpty(oaSysSendMessage.getDriverOrderEarlyWarning())) {
            oaSysSendMessage.setDriverOrderEarlyWarning(0);
        }
        if (oaSysSendMessage.getOfficeId()==null){
            Long userId = TokenUtil.getTokenUserId();
            QueryWrapper<OaSysUser> oaSysUserQueryWrapper = new QueryWrapper<>();
            oaSysUserQueryWrapper.eq(OaSysUser.SYS_USER_ID, userId);
            OaSysUser oaSysUser = oaSysUserMapper.selectOne(oaSysUserQueryWrapper);
            oaSysSendMessage.setOfficeId(oaSysUser.getOfficeId());
        }
        int re = 0;
        if (flag) {
            re = oaSysSendMessageMapper.updateById(oaSysSendMessage);
        } else {
            re = oaSysSendMessageMapper.insert(oaSysSendMessage);
        }
        if (re != 0) {
            return Rv.wrap("操作成功！");
        } else {
            return Rv.error(EErrorCode.addError);
        }
    }

    @Override
    public Rv getAddOrUpdateManagerInfo(OaSysManager oaSysManager) {
        if (CheckUtil.objIsEmpty(oaSysManager.getCityId(), oaSysManager.getManagerPhone())) {
            return Rv.error(EErrorCode.missingArg);
        }
        Long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId)) {
            return Rv.error(EErrorCode.missingAuthArgs);
        }
        Date date = new Date();
        //司管id --主键id
        Long managerId = oaSysManager.getManagerId();
        if (CheckUtil.objIsEmpty(managerId)) {//添加数据
            //验证是否这个城市已存在司管信息，不许添加
            QueryWrapper<OaSysManager> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(OaSysManager.CITY_ID, oaSysManager.getCityId());
            queryWrapper.eq(OaSysManager.DELETED, 0);
            List<OaSysManager> oaSysManagerList = oaSysManagerMapper.selectList(queryWrapper);
            if (oaSysManagerList.size() > 0) {
                return Rv.error(EErrorCode.managerInfoExistError);
            }
            OaSysManager newInfo = new OaSysManager();
            newInfo.setManagerId(IDUtil.nextId());
            newInfo.setCityId(oaSysManager.getCityId());
            newInfo.setCityName(oaSysManager.getCityName());
            newInfo.setManagerPhone(oaSysManager.getManagerPhone());
            newInfo.setSupplierId(oaSysManager.getSupplierId());
            newInfo.setDeleted(oaSysManager.getDeleted());
            newInfo.setUpdateUser(userId);
            newInfo.setUpdateTime(date);
            newInfo.setCreateUser(userId);
            newInfo.setCreateTime(date);
            oaSysManagerMapper.insert(newInfo);
        } else {//修改数据
            OaSysManager newInfo = new OaSysManager();
            newInfo.setManagerId(managerId);
            newInfo.setCityId(oaSysManager.getCityId());
            newInfo.setCityName(oaSysManager.getCityName());
            newInfo.setManagerPhone(oaSysManager.getManagerPhone());
            newInfo.setSupplierId(oaSysManager.getSupplierId());
            newInfo.setDeleted(oaSysManager.getDeleted());
            newInfo.setUpdateUser(userId);
            newInfo.setUpdateTime(date);
            oaSysManagerMapper.updateById(newInfo);
        }
        return Rv.wrap();
    }


}
